package ua.org.extjsbuilder.parser;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;

public class JSParser {
	private static final int BUFFER_SIZE = 1024;
	private Reader input;
	
	public JSParser(Reader input){
		this.input = input;
	}
	
	public JSParser(InputStream input) {
		this.input = new InputStreamReader(input);
	}

	public void parse(JSErrorHandler handler){
		//TODO: implement actual parsing
	}

	public void compile(Writer out, boolean debug, boolean optimize) {
		if(debug){
			writeDebugVersion(out);
			return;
		}
		throw new RuntimeException("Not supported set of options");
		//TODO: implement compilation
	}

	private void writeDebugVersion(Writer out) {
		try {
			int len = 0;
			char[] cbuf = new char[BUFFER_SIZE];
			while((len=input.read(cbuf))>0){
				out.write(cbuf,0,len);
			}
		}
		catch(IOException ex){
			throw new JSParseException(ex);
		}
	}
}
